﻿using System;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

namespace GameLauncher.Runtime
{
    public enum MessageShowType
    {
        OneBtn,
        TwoBtn
    }

    public class HotFixUIWindow : MonoBehaviour
    {
        private void Start()
        {
            mBtnClear.onClick.AddListener(OnClickClearBtn);
            RefreshVersion();
        }
        
        private void OnDestroy()
        {
            PauseDownload();
            mSureBtn.onClick.RemoveAllListeners();
            mCancelBtn.onClick.RemoveAllListeners();
        }

        /// <summary>
        /// 下载进度更新
        /// </summary>
        /// <param name="progress"></param>
        public void RefreshDownProgress(float progress)
        {
            mSProgress.gameObject.SetActive(true);
            mSProgress.value = progress;
        }

        /// <summary>
        /// 下载进度完成
        /// </summary>
        /// <param name="type"></param>
        public void RefreshDownloadComplete()
        {
            Debug.Log("DownLoad_Complete");
        }

        /// <summary>
        /// 刷新描述信息
        /// </summary>
        /// <param name="desc"></param>
        public void RefreshDescLabel(string desc)
        {
            mTxtDesc.gameObject.SetActive(true);
            mTxtDesc.text = desc;
        }

        /// <summary>
        /// 刷新版本信息
        /// </summary>
        public void RefreshVersion()
        {
            mTxtAppVersion.text = "TODO Version";
            mTxtResVersion.text = "TODO Version";
        }
        
        public void ShowOneMessageBox(string desc, Action btnAction)
        {
            ShowMessageBox(MessageShowType.OneBtn, desc, btnAction, null);
        }

        public void ShowTwoMessageBox(string desc, Action sureBtnAction, Action cancelBtnAction)
        {
            ShowMessageBox(MessageShowType.TwoBtn, desc, sureBtnAction, cancelBtnAction);
        }





        private void OnClickClearBtn()
        {
            PauseDownload();
            //ShowTwoMessageBox(HotFixLabel.Instance.LabelClear, () => { });
            // ShowMessageBox(HotFixLabel.Instance.LabelClear, MessageShowType.TwoButton,
            //     HotFixMessageBoxStyle.StyleEnum.Style_Clear,
            //     () =>
            //     {
            //         //TODO Clear
            //         Application.Quit();
            //     }, () => { ResumeDownload(); });
        }


        private void ResumeDownload()
        {
            // TODO 重启下载
        }

        private void PauseDownload()
        {
            // TODO 暂停下载
        }

        private void ShowMessageBox(MessageShowType type, string desc, Action sureBtnAction, Action cancelBtnAction)
        {
            mCancelBtn.gameObject.SetActive(type == MessageShowType.TwoBtn);
            mTxtMessageBoxDesc.text = desc;
            mMessageBoxObj.SetActive(true);
            mSureBtn.onClick.RemoveAllListeners();
            mCancelBtn.onClick.RemoveAllListeners();
            mSureBtn.onClick.AddListener(() =>
            {
                sureBtnAction?.Invoke();
                mMessageBoxObj.SetActive(false);
            });

            mCancelBtn.onClick.AddListener(() =>
            {
                cancelBtnAction?.Invoke();
                mMessageBoxObj.SetActive(false);
            });
        }
        
        
        [SerializeField]private Slider mSProgress;
        [SerializeField]private Text mTxtDesc;
        [SerializeField]private Button mBtnClear;
        [SerializeField]private Text mTxtAppVersion;
        [SerializeField]private Text mTxtResVersion;
        [SerializeField]private GameObject mMessageBoxObj;
        [SerializeField]private Button mSureBtn;
        [SerializeField]private Button mCancelBtn;
        [SerializeField]private Text mTxtMessageBoxDesc;
    }
}